package com.zhuiYun.project.util;

import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

public class MailUtils {


    private static JavaMailSender mailSender = SpringContextUtil.getBean(JavaMailSender.class);

    /**
     * 封装发送邮箱
     * lzy 2022-8-22 19点43分
     * @param mail  邮箱号
     * @param subjuect  发送标题
     * @param text  发送内容
     */
    public static void sendMail (String mail,String subjuect, String text) {
        int count = 1;//默认发送一次
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(mimeMessage, true);
            while (count-- != 0) {
                //标题4
                helper.setSubject(subjuect);
                //内容
                helper.setText(text,true);
                //邮件接收者
                helper.setTo(mail);
                //邮件发送者，必须和配置文件里的一样，不然授权码匹配不上
                helper.setFrom("389641604@qq.com");
                mailSender.send(mimeMessage);
            }
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

}
